package com.example.imchat.controller.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Looper;
import android.widget.ListView;
import android.widget.Toast;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.example.imchat.R;
import com.example.imchat.controller.adapter.InviteAdapter;
import com.example.imchat.model.Model;
import com.example.imchat.model.bean.InvivationInfo;
import com.example.imchat.utils.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

public class InviteInfoActivity extends Activity {
    private ListView lv_invite;
    private InviteAdapter inviteAdapter;
    private  LocalBroadcastManager mLBM;
    private InviteAdapter.OnInviteListener mOnInviteListener = new InviteAdapter.OnInviteListener() {
        @Override
        public void onAccept(InvivationInfo invationInfo) {
            //通知服务器 接受了添加好友
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().acceptInvitation(invationInfo.getUser().getHxid());
                        //数据库更新
                        Model.getInstance().getDbManager().getInviteTableDao().updateInvitationStatus(InvivationInfo.InvitationStatus.INVITE_ACCEPT,invationInfo.getUser().getHxid());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //页面变化
                                Toast.makeText(InviteInfoActivity.this,"接受了邀请",Toast.LENGTH_SHORT).show();
                                //刷新页面

                                refresh();
                            }
                        });

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"接受邀请失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                }
            });
        }
            //拒绝了添加好友
        @Override
        public void onReject(InvivationInfo invationInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().declineInvitation(invationInfo.getUser().getHxid());
                        //数据库更新
                        Model.getInstance().getDbManager().getInviteTableDao().removeInvitation(invationInfo.getUser().getHxid());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //页面变化
                                Toast.makeText(InviteInfoActivity.this,"拒绝成功",Toast.LENGTH_SHORT).show();
                                //刷新页面
                                refresh();
                            }
                        });


                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"拒绝邀请失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }



        //接受邀请按钮
        @Override
        public void onInviteAccept(final InvivationInfo invitationInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //告诉环信服务器接受了邀请
                        EMClient.getInstance().groupManager().acceptInvitation(invitationInfo.getGroup().getGroupId(),invitationInfo.getGroup().getInvatePerson());

                        //本地数据库更新
                        invitationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_ACCEPT_INVITE);
                        Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

                        //内存数据变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"接受邀请",Toast.LENGTH_SHORT).show();

                                //刷新页面
                                refresh();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"接受邀请失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }


        //拒绝了群邀请
        @Override
        public void onInviteReject(InvivationInfo invationInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //去环信服务器接受邀请
                        EMClient.getInstance().groupManager().declineInvitation(invationInfo.getGroup().getGroupId(),invationInfo.getGroup().getInvatePerson(),"对方嫌你长得太丑并拒绝了你的群邀请");
                        //本地数据更新
                        invationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_REJECT_INVITE);
                        Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);
                        //页面数据变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"拒绝了群邀请",Toast.LENGTH_SHORT).show();
                                //刷新页面
                                refresh();
                            }
                        });

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        Toast.makeText(InviteInfoActivity.this,"拒绝群邀请失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }


        //同意了群申请
        @Override
        public void onApplicationAccept(InvivationInfo invationInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //去环信服务器接受邀请
                        EMClient.getInstance().groupManager().acceptApplication(invationInfo.getGroup().getGroupId(),invationInfo.getGroup().getInvatePerson());
                        //本地数据更新
                        invationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED);
                        Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);
                        //页面数据变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"同意了群邀请",Toast.LENGTH_SHORT).show();
                                //刷新页面
                                refresh();
                            }
                        });

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        Toast.makeText(InviteInfoActivity.this,"同意群申请失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }


        //拒绝了群申请
        @Override
        public void onApplicationReject(InvivationInfo invationInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //去环信服务器接受邀请
                        EMClient.getInstance().groupManager().declineApplication(invationInfo.getGroup().getGroupId(),invationInfo.getGroup().getInvatePerson(),"拒绝申请");
                        //本地数据更新
                        invationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_REJECT_APPLICATION);
                        Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);
                        //页面数据变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteInfoActivity.this,"拒绝了群邀请",Toast.LENGTH_SHORT).show();
                                //刷新页面
                                refresh();
                            }
                        });

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        Toast.makeText(InviteInfoActivity.this,"拒绝群申请失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    };
    private BroadcastReceiver InviteChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //刷新页面
            refresh();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_info);

        initView();
        initDate();
    }

    private void initDate() {
        inviteAdapter = new InviteAdapter(this,mOnInviteListener);

        lv_invite.setAdapter(inviteAdapter);
        //刷新   获取数据库中的所有邀请
         refresh();

         //邀请信息变化的广播  在邀请界面实时更新 信息
         mLBM = LocalBroadcastManager.getInstance(this);
         mLBM.registerReceiver(InviteChangeReceiver,new IntentFilter(Constant.CONTACT_INVITE_CHANGED));
         mLBM.registerReceiver(InviteChangeReceiver,new IntentFilter(Constant.GROUP_INVITE_CHANGED));

    }
    private void  refresh(){
        List<InvivationInfo> invitations = Model.getInstance().getDbManager().getInviteTableDao().getInvitations();
        inviteAdapter.refresh(invitations);
    }



    private void initView() {
        lv_invite = (ListView)findViewById(R.id.lv_invite);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLBM.unregisterReceiver(InviteChangeReceiver);
    }
}
